import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/config/hc_default_theme_config.dart';
import 'package:hc_ui/theme/hc_base_theme.dart';

import 'hc_common_theme.dart';

class HcTagTheme extends HcBaseTheme<HcTagTheme> {
  BorderRadius? _borderRadius;
  EdgeInsets? _padding;
  EdgeInsets? _margin;
  Size? _minSize;
  Size? _maxSize;
  double? _borderWidth;
  double? _iconSpacing;
  double? _iconSize;
  double? _fontSize;
  double? _fontHeight;
  FontWeight? _fontWeight;

  HcTagTheme({
    BorderRadius? borderRadius,
    EdgeInsets? padding,
    EdgeInsets? margin,
    Size? minSize,
    Size? maxSize,
    double? borderWidth,
    double? iconSpacing,
    double? iconSize,
    double? fontSize,
    double? fontHeight,
    FontWeight? fontWeight,
  })  : _borderRadius = borderRadius,
        _padding = padding,
        _margin = margin,
        _minSize = minSize,
        _maxSize = maxSize,
        _borderWidth = borderWidth,
        _iconSpacing = iconSpacing,
        _iconSize = iconSize,
        _fontSize = fontSize,
        _fontWeight = fontWeight,
        _fontHeight = fontHeight;

  @override
  void initTheme({HcCommonTheme? tempTheme}) {
    super.initTheme(tempTheme: tempTheme);
    HcTagTheme tagTheme = HcCustomerConfig().allTheme.tagTheme;
    _borderRadius ??= tagTheme.borderRadius;
    _padding ??= EdgeInsets.symmetric(
        vertical: commonTheme.vSpacingXxs, horizontal: commonTheme.hSpacingXxs);
    _margin ??= EdgeInsets.zero;
    _minSize ??= tagTheme.minSize;
    _maxSize ??= tagTheme.maxSize;
    _borderWidth ??= commonTheme.lineBase;
    _iconSpacing ??= commonTheme.hSpacingXxs;
    _iconSize ??= tagTheme.iconSize;
    _fontHeight ??= commonTheme.fontHeightLg;
    _fontWeight ??= tagTheme.fontWeight;
    _fontSize ??= tagTheme.fontSize;
  }

  @override
  HcTagTheme merge(HcTagTheme? value) {
    if (value == null) return this;
    return copyWith(
      borderRadius: value._borderRadius,
      borderWidth: value._borderWidth,
      padding: value._padding,
      margin: value._margin,
      minSize: value._minSize,
      maxSize: value._maxSize,
      iconSize: value._iconSize,
      iconSpacing: value._iconSpacing,
      fontHeight: value._fontHeight,
      fontSize: value._fontSize,
      fontWeight: value._fontWeight,
    );
  }

  HcTagTheme copyWith({
    BorderRadius? borderRadius,
    EdgeInsets? padding,
    EdgeInsets? margin,
    Size? minSize,
    Size? maxSize,
    double? borderWidth,
    double? iconSpacing,
    double? iconSize,
    double? fontSize,
    double? fontHeight,
    FontWeight? fontWeight,
  }) {
    Size finalMaxSize = Size(
        max((minSize ?? this.minSize).width, (maxSize ?? this.minSize).width),
        max((minSize ?? this.maxSize).height,
            (maxSize ?? this.maxSize).height));
    return HcTagTheme(
      borderRadius: borderRadius ?? _borderRadius,
      padding: padding ?? _padding,
      margin: margin ?? _margin,
      minSize: minSize ?? _minSize,
      maxSize: finalMaxSize,
      borderWidth: borderWidth ?? _borderWidth,
      iconSpacing: iconSpacing ?? _iconSpacing,
      iconSize: iconSize ?? _iconSize,
      fontWeight: fontWeight ?? _fontWeight,
      fontSize: fontSize ?? _fontSize,
      fontHeight: fontHeight ?? _fontHeight,
    );
  }

  BorderRadius get borderRadius =>
      _borderRadius ?? HcDefaultThemeConfig.defaultTagTheme.borderRadius;

  Size get maxSize => _maxSize ?? HcDefaultThemeConfig.defaultTagTheme.maxSize;

  Size get minSize => _minSize ?? HcDefaultThemeConfig.defaultTagTheme.minSize;

  double get borderWidth =>
      _borderWidth ?? HcDefaultThemeConfig.defaultTagTheme.borderWidth;

  EdgeInsets get margin =>
      _margin ?? HcDefaultThemeConfig.defaultTagTheme.margin;

  EdgeInsets get padding =>
      _padding ?? HcDefaultThemeConfig.defaultTagTheme.padding;

  double get iconSize =>
      _iconSize ?? HcDefaultThemeConfig.defaultTagTheme.iconSize;

  double get iconSpacing =>
      _iconSpacing ?? HcDefaultThemeConfig.defaultTagTheme.iconSpacing;

  FontWeight get fontWeight =>
      _fontWeight ?? HcDefaultThemeConfig.defaultTagTheme.fontWeight;

  double get fontHeight =>
      _fontHeight ?? HcDefaultThemeConfig.defaultTagTheme.fontHeight;

  double get fontSize =>
      _fontSize ?? HcDefaultThemeConfig.defaultTagTheme.fontSize;
}
